import numpy as np
import matplotlib.pyplot as plt

scores = np.array([
    [88, 92, 85, 91],
    [75, np.nan, 79, 84],
    [90, 95, np.inf, 97],
    [60, 65, 58, 62],
    [82, 80, 85, 88],
    [np.nan, 72, 78, 70]
])

#处理异常值
for i in range(6):
    row = scores[i]
    valid = row[~np.isnan(row) & ~np.isinf(row)]
    if np.any(np.isnan(row)):
        row[np.isnan(row)] = np.mean(valid)
    if np.any(np.isinf(row)):
        row[np.isinf(row)] = np.max(valid)

#计算统计量
stu_avg = np.mean(scores, 1)
course_avg = np.mean(scores, 0)
max_val, min_val, std_val = np.max(scores), np.min(scores), np.std(scores)

#排序和筛选
sort_idx = np.argsort(-stu_avg)
class_avg = np.mean(stu_avg)
low_idx = stu_avg < class_avg

print(f"统计结果：最高{max_val}分 最低{min_val}分 标准差{std_val:.2f}")
print("课程均分：", [f"{x:.1f}" for x in course_avg])
print("低于均分的学生：", np.where(low_idx)[0] + 1)

#画图
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(8,4))
plt.bar(range(6), stu_avg[sort_idx], color='skyblue')
plt.title('学生平均成绩排名')
plt.xticks(range(6), [f'第{i+1}名' for i in range(6)])
plt.ylabel('分数')
plt.show()